package com.dzxcqmxm.servlet.user;

import com.dzxcqmxm.Dao.UserDao;
import com.dzxcqmxm.Dao.impl.UserDaoImpl;
import com.dzxcqmxm.JB.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author Payboi
 * @create 2022/6/2-21:59
 * @FileName addUserServlet
 * @Description
 */
@WebServlet({"/addUserServlet","/registerServlet"})
public class addUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao userDao = new UserDaoImpl();
        User user = new User();

        //获取我的输入
        user.setUserName(new String(request.getParameter("UserName" ).getBytes("iso-8859-1"),"UTF-8"));
        user.setUserPwd(new String(request.getParameter("UserPwd" ).getBytes("iso-8859-1"),"UTF-8"));

        try {
            userDao.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
         * 请求转发的路径/，已经包括了，本项目的网络名，也就是一个斜杠等价于：http://localhost:8901/dzxc/
         */
        String uri = request.getRequestURI();
       if(uri.endsWith("addUserServlet")){
           request.getRequestDispatcher("/admin/user/addUser.jsp").forward(request,response);

       }else if(uri.endsWith("registerServlet")){
           request.setAttribute("result","注册成功！");
           request.getRequestDispatcher("/login/register.jsp").forward(request,response);
       }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}